/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.tunnel.db.dataobject;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Author xujin
 * @create 2023/4/5 21:31
 */
@TableName("t_identity_source_sync_record")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class IdentitySourceSyncRecordDO implements Cloneable, Serializable {

    private Long id;

    private Long syncHistoryId;

    private String actionType;

    private String objectId;

    private String objectName;

    private String objectType;

    private String status;

    private String message;

    private String isDeleted;

    private LocalDateTime createAt;

    private LocalDateTime updateAt;

    private String createBy;

    private String updateBy;

    private static final long serialVersionUID = 1L;

    @Override
    public IdentitySourceSyncRecordDO clone() throws CloneNotSupportedException {
        return (IdentitySourceSyncRecordDO) super.clone();
    }

    public enum Column {
        id("id", "id", "INT", false),
        syncHistoryId("sync_history_id", "syncHistoryId", "INT", false),
        actionType("action_type", "actionType", "VARCHAR", false),
        objectId("object_id", "objectId", "VARCHAR", false),
        objectName("object_name", "objectName", "VARCHAR", false),
        objectType("object_type", "objectType", "VARCHAR", false),
        status("status", "status", "VARCHAR", false),
        message("message", "message", "VARCHAR", false),
        enabled("enabled", "enabled", "INTEGER", false),
        isDeleted("is_deleted", "isDeleted", "VARCHAR", false),
        createAt("create_at", "createAt", "TIMESTAMP", false),
        updateAt("update_at", "updateAt", "TIMESTAMP", false),
        createBy("create_by", "createBy", "VARCHAR", false),
        updateBy("update_by", "updateBy", "VARCHAR", false),
        ;

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;

        public String value() {
            return this.column;
        }

        public String getValue() {
            return this.column;
        }

        public String getJavaProperty() {
            return this.javaProperty;
        }

        public String getJdbcType() {
            return this.jdbcType;
        }

        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        public static Column[] excludes(Column... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        public static Column[] all() {
            return Column.values();
        }

        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }

        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }

    public enum ActionType {

        INSERT("insert", "新增"),
        UPDATE("update", "修改"),
        DELETE("delete", "删除");

        private final String value;

        private final String name;

        ActionType(String value, String name) {
            this.value = value;
            this.name = name;
        }

        public String getValue() {
            return this.value;
        }

        public String value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }

        public static ActionType parseValue(String value) {
            if (value != null) {
                for (ActionType item : values()) {
                    if (item.value.equals(value)) {
                        return item;
                    }
                }
            }
            return null;
        }

        public static ActionType parseName(String name) {
            if (name != null) {
                for (ActionType item : values()) {
                    if (item.name.equals(name)) {
                        return item;
                    }
                }
            }
            return null;
        }
    }

    public enum ObjectType {

        USER("user", "用户"),
        DEPART("depart", "部门");

        private final String value;

        private final String name;

        ObjectType(String value, String name) {
            this.value = value;
            this.name = name;
        }

        public String getValue() {
            return this.value;
        }

        public String value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }

        public static ObjectType parseValue(String value) {
            if (value != null) {
                for (ObjectType item : values()) {
                    if (item.value.equals(value)) {
                        return item;
                    }
                }
            }
            return null;
        }

        public static ObjectType parseName(String name) {
            if (name != null) {
                for (ObjectType item : values()) {
                    if (item.name.equals(name)) {
                        return item;
                    }
                }
            }
            return null;
        }
    }

    public enum Status {

        SUCCESS("success", "成功"),
        FAIL("fail", "失败"),
        SKIP("skip", "跳过"),
        PENDING("pending", "同步中");

        private final String value;

        private final String name;

        Status(String value, String name) {
            this.value = value;
            this.name = name;
        }

        public String getValue() {
            return this.value;
        }

        public String value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }

        public static Status parseValue(String value) {
            if (value != null) {
                for (Status item : values()) {
                    if (item.value.equals(value)) {
                        return item;
                    }
                }
            }
            return null;
        }

        public static Status parseName(String name) {
            if (name != null) {
                for (Status item : values()) {
                    if (item.name.equals(name)) {
                        return item;
                    }
                }
            }
            return null;
        }
    }

}
